package com.nna88.voz.main;

import java.util.ArrayList;
import java.util.Iterator;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.nna88.voz.contain.containPage2;
import com.nna88.voz.listview.listViewCustom2;
import com.nna88.voz.listview.listViewCustom2.OnActionItemClickListener;

public class Page2 extends Page{
	private ArrayList<containPage2> ListContains;
	private listViewCustom2 adapter;
	private String mTextTitle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ListContains = new ArrayList<containPage2>();
		adapter = new listViewCustom2(mActivity,ListContains);
	    adapter.setSize(mTextSize);
	    mList.setAdapter(adapter);
	    //log(URL+getIntent().getStringExtra("URL"));
	    mTextTitle = getIntent().getStringExtra("TITLE");
	    mMenuTitle.setText(mTextTitle);
	    mParser.setUrl(URL+getIntent().getStringExtra("URL"));
	    mTask.execute(TASK_GETDOC);
	    
	    adapter.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(int pos, int actionId,int state) {
				// TODO Auto-generated method stub
				if(state == 0){
					Intent i = null;
					if(ListContains.get(pos).isUrl()==0){ //forum
						i = new Intent(mContext, Page2.class);
					}else								//thread									
						i = new Intent(mContext, Page3.class); 
					if(actionId == 1){	
						i.putExtra("URL", ListContains.get(pos).UrlThread());
					}else if(actionId == 2){ 
						i.putExtra("URL", ListContains.get(pos).UrlLastPosst());
					}
					i.putExtra("TITLE", ListContains.get(pos).Thread());
					startActivity(i);
					overridePendingTransition(R.anim.rail, R.anim.rail);
				}else{
					GoPage(state);
				}
			}
		});
	    
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		updateSetFont(mTextSize);
	}
	@Override
	protected void updateSetFont(float value) {
		// TODO Auto-generated method stub
		super.updateSetFont(value);
		adapter.setSize(value);
		adapter.notifyDataSetChanged();
	}

	@Override
	void Parser(int taskType) {
		// TODO Auto-generated method stub
		super.Parser(taskType);
		if(doc == null){
			toast("Cannot connect Vozforums",1,0);
			return;
		}
		ListContains.clear();
    	Element eleTag1,eleTag1Image ,eleTag1Time ,eleTag1Replies,eleTag1Views ;
		Element eleLogin = doc.select("a[href=private.php]").first();
		mTextNumPage.setText(parsePage(GO_INDEXPAGE));
		if(mTextNumPage.getText() != null)	mMenuTitle.setText(mTextTitle+" - "+mTextNumPage.getText());
		if(eleLogin != null){
			String temp = doc.select("td[class=alt2]").get(0).select("a[href*=mem]").attr("href");
			temp = temp.split("=")[1];
			mUser.setUserId(temp);
		}
		Bitmap bitmap = null;
		String urlImage,text1 = null,text2=null,text3=null,text4=null,text1_url1=null,text2_url2=null;
		Element mainElement = doc.select("tbody:has(form[id])").first();
		if(mainElement == null) return;
		Element eletbody = mainElement.select("tbody[id*=threadbits_forum]").first();
		Elements eleTag1s = eletbody.select("tr:has(td[id*=td_threads])");
		Elements eleTag2s = mainElement.select("tr > td[class=alt1Active]");
		Element eleText1 = null;
		Iterator<Element> iteTag1 = eleTag1s.iterator();
		Iterator<Element> iteTag2 = eleTag2s.iterator();
		containPage2 contain;
		if(iteTag2.hasNext())	ListContains.add(new containPage2("Sub-Forum",null,null,null,null,null,null));
		while(iteTag2.hasNext()){
			eleTag1 = iteTag2.next();
			eleTag1Image = eleTag1.select("td > img[id]").first();
			eleTag1Time = eleTag1.nextElementSibling();
			eleTag1Replies = eleTag1Time.nextElementSibling();
			eleTag1Views = eleTag1Replies.nextElementSibling();
			if(eleTag1Image != null){ 
				urlImage= eleTag1Image.select("img").attr("src");
				bitmap = loadBitmapAssert(urlImage); 
			}
			if(eleTag1 != null) text1 = eleTag1.text();
			if(eleTag1.select("div > a").first() != null) 
				text1_url1 = eleTag1.select("div > a").first().attr("href");
			if(eleTag1Time.select("span > a").first() != null){
				text2 = eleTag1Time.select("span > a").first().text();
				text2_url2 = eleTag1Time.select("span > a").first().attr("href");
			}
			if(eleTag1Replies != null)		text3 = eleTag1Replies.text();
			if(eleTag1Views != null) text4 = eleTag1Views.text();
			contain = new containPage2(text1,text2, text3,text4,	bitmap,	text1_url1, text2_url2);
			ListContains.add(contain);
			adapter.notifyDataSetChanged();
		}
		ListContains.add(new containPage2("Thread",null,null,null,null,null,null));
		while(iteTag1.hasNext()){
			text2_url2 = null;
			eleTag1 = iteTag1.next();
			eleText1 = eleTag1.select("a[id*=thread_title").first();
			if( eleText1!= null){
				
				text1 = eleTag1.select("a[id*=thread_title").first().text();
				text1_url1 = eleTag1.select("a[id*=thread_title").first().attr("href");
			}
			if(eleTag1.select("td[class=alt2]:has(span)").first() != null){
				text2 = eleTag1.select("td[class=alt2]:has(span)").first() .text();
			}
			if(eleTag1.select("td[align=center]").first() != null){
				text3 = eleTag1.select("td[align=center]").get(0).text();
				text4 = eleTag1.select("td[align=center]").get(1).text();
			}
			if(eleTag1.select("img[id*=thread_statusicon]").first() != null){
				bitmap = loadBitmapAssert(eleTag1.select("img[id*=thread_statusicon]").first().attr("src")); 
			}
			if(eleTag1.select("a[id*=thread_gotonew]").first() != null){
				text2_url2 = eleTag1.select("a[id*=thread_gotonew]").first().attr("href");
			}
			contain = new containPage2(text1,text2,text3,text4,bitmap,text1_url1,text2_url2);
			
			if(eleText1.hasClass("vozsticky")){
				contain.setSticky(true);
			}else{
				contain.setSticky(false);
			}
			ListContains.add(contain);
			log(text1_url1 +";"+text2_url2);
			adapter.notifyDataSetChanged();
		}
		
	}

}
